package com.bsoft.gol.hcs.clinical.dao;

import com.bsoft.gol.hcs.clinical.bean.response.QueryMedicalSkillTotalResp;
import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.util.annotation.RpcService;
import ctd.util.converter.ConversionUtils;
import hcn.base.ExamineRequisitionRecord;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import org.hibernate.criterion.CriteriaSpecification;

import java.util.List;
import java.util.Map;

public abstract class ExamineRequisitionRecordDAO extends HibernateSupportDelegateDAO<ExamineRequisitionRecord> {
    public ExamineRequisitionRecordDAO() {
        this.setKeyField("examineRequisitionRecordId");
    }

    /**
     * 获取检验列表
     *
     * @param consultId
     * @return
     */
    @RpcService
    @DAOMethod(sql = " from ExamineRequisitionRecord where consultId =:consultId ")
    public abstract List<ExamineRequisitionRecord> queryExamineRequisitionRecord(@DAOParam("consultId") String consultId);

    @RpcService
    @DAOMethod(sql = " from ExamineRequisitionRecord where examineRequisitionRecordId =:examineRequisitionRecordId ")
    public abstract ExamineRequisitionRecord getExamineRequisitionRecordById(@DAOParam("examineRequisitionRecordId") String examineRequisitionRecordId);

    @RpcService
    @DAOMethod(sql = " from ExamineRequisitionRecord where appointsNo =:appointsNo ")
    public abstract List<ExamineRequisitionRecord> findExamineRequisitionRecordByAppointsNo(@DAOParam("appointsNo") String appointsNo);

    @RpcService
    @DAOMethod(sql = " from ExamineRequisitionRecord where applicationNo =:applicationNo ")
    public abstract ExamineRequisitionRecord getExamineRequisitionRecordByApplicationNo(@DAOParam("applicationNo") String applicationNo);


    @RpcService
    @DAOMethod(sql = "delete from ExamineRequisitionRecord where " +
            "examineRequisitionRecordId=:examineRequisitionRecordId")
    public abstract void deleteExamineRequisitionRecord(@DAOParam(value = "examineRequisitionRecordId") String examineRequisitionRecordId);

    @RpcService
    @DAOMethod(sql = "update ExamineRequisitionRecord set state='1',submissionTime=now() where " +
            "examineRequisitionRecordId =:examineRequisitionRecordId")
    public abstract void updateExamineRequisitionRecordState(@DAOParam("examineRequisitionRecordId") String examineRequisitionRecordId);

    @RpcService
    @DAOMethod(sql = "update ExamineRequisitionRecord set state='0' where examineRequisitionRecordId " +
            "=:examineRequisitionRecordId")
    public abstract void updateExamineRequisitionRecordReturnState(@DAOParam("examineRequisitionRecordId") String examineRequisitionRecordId);

    @RpcService
    @DAOMethod(sql = "update ExamineRequisitionRecord set state='0',applicationNo = '' where applicationNo " +
            "=:applicationNo")
    public abstract void updateExamineRequisitionRecordReserveState(@DAOParam("applicationNo") String applicationNo);

    @RpcService
    @DAOMethod(sql = "update ExamineRequisitionRecord set state=:state where applicationNo " +
            "=:applicationNo")
    public abstract void updateExamineRequisitionRecordStateByApplicationNo(@DAOParam("applicationNo") String applicationNo, @DAOParam("state") Byte state);

    @RpcService
    @DAOMethod(sql = "update ExamineRequisitionRecord set state = :state,appointsNo = :appointsNo where " +
            "applicationNo in :applicationNos ")
    public abstract void updateStateAndAppointsNo(@DAOParam("state") Byte state,
                                                  @DAOParam("appointsNo") String appointsNo,
                                                  @DAOParam("applicationNos") List<String> applicationNos);

    @RpcService
    @DAOMethod(sql = "update ExamineRequisitionRecord set appointsNo = :newAppointsNo where " +
            "appointsNo = :appointsNo")
    public abstract void updateAppointsNo(@DAOParam("appointsNo") String appointsNo,
                                          @DAOParam("newAppointsNo") String newAppointsNo);

    @RpcService
    @DAOMethod(sql = "update ExamineRequisitionRecord set state = :state,appointsNo = null where " +
            "appointsNo = :appointsNo")
    public abstract void updateStateByAppointsNo(@DAOParam("state") Byte state,
                                                 @DAOParam("appointsNo") String appointsNo);

    @RpcService
    @DAOMethod(sql = " update ExamineRequisitionRecord set payStatus=:payStatus where applicationNo in:applicationNoList")
    public abstract void updatePayStatus(@DAOParam("applicationNoList") List<String> applicationNoList, @DAOParam("payStatus") String payStatus);


    /**
     * @description: 修改申请单为已报告
     * @author: chenJij@bsoft.com.cn
     */
    @RpcService
    @DAOMethod(sql = " update ExamineRequisitionRecord set state='4' where organizationId=:organizationId and applicationNo=:applicationNo")
    public abstract void updateReportStatus(@DAOParam("organizationId") String organizationId, @DAOParam("applicationNo") String applicationNo);

    /**
     * 获取不同状态总数
     *
     * @param consultId
     * @return
     */
    @RpcService
    public QueryMedicalSkillTotalResp queryExamineRequisitionTotal(final String consultId) {
        HibernateStatelessResultAction<QueryMedicalSkillTotalResp> action =
                new AbstractHibernateStatelessResultAction<QueryMedicalSkillTotalResp>() {
                    @Override
                    public void execute(StatelessSession ss) throws Exception {
                        StringBuffer sql = new StringBuffer();
                        sql.append("select SUM(CASE WHEN state=0 THEN 1 ELSE 0 END) as uncommittedNum,");
                        sql.append("SUM(CASE WHEN state=1 THEN 1 ELSE 0 END) as committedNum,");
                        sql.append("SUM(CASE WHEN state=2 THEN 1 ELSE 0 END) as reservedNum,");
                        sql.append("SUM(CASE WHEN state=3 THEN 1 WHEN state=4 THEN 1 ELSE 0 END) as executedNum FROM " +
                                "examine_requisition_record ");
                        sql.append("where consult_id=:consultId ");
                        Query query = ss.createSQLQuery(sql.toString());
                        query.setParameter("consultId", consultId);
                        query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                        List<Map> list = query.list();
                        if (list.size() == 1) {
                            Object o = list.get(0);
                            QueryMedicalSkillTotalResp queryMedicalSkillTotalResp = ConversionUtils.convert(o,
                                    QueryMedicalSkillTotalResp.class);
                            setResult(queryMedicalSkillTotalResp);
                        }
                    }
                };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }
}
